package leetcode;

import java.util.Stack;

/**
 * Created by Ghost on 2015/2/5 0005.
 */
public class Longest_Valid_Parentheses {
    class Package{
        char ch;
        int idx;
        Package(char ch,int idx){
            this.ch = ch;
            this.idx = idx;
        }
    }
    public int longestValidParentheses(String s) {
        if(s==null || s.length()==0)
            return 0;
        Stack<Package> stack = new Stack<Package>();
        char[] ss = s.toCharArray();
        boolean[] sb = new boolean[ss.length];
        int idx=0;
        int max = 0;
        for (Character c : ss) {

            if (c == '('){
                stack.push(new Package(c,idx));
            }
            else{
                if(!stack.empty() && '('==stack.peek().ch){
                    Package p = stack.pop();

                    sb[idx] = true;
                    sb[p.idx] = true;
                }
            }
            idx++;
        }
        int count=0;
        if(sb[0])
            count=1;
        for(int i=1;i<sb.length;i++){
            if(sb[i]==sb[i-1] && sb[i])
                count++;
            else{
                max = max<count?count:max;
                if(sb[i]){
                    count=1;
                }
            }
        }
        return max<count?count:max;
    }

    public static void main(String[] args){
        Longest_Valid_Parentheses longest_valid_parentheses = new Longest_Valid_Parentheses();
        System.out.println(longest_valid_parentheses.longestValidParentheses(""));
    }
}
